﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace NewBee.Core.Data.SQLBuilder
{
    public class Delete
    {
        private static string DELETE_SQL = @"DELETE FROM {0} WHERE {1}";
        private DbHelper _DbHelper = null;

        private string _TableName = string.Empty;
        private string _Where = string.Empty;
        private object _Parameters;
        public Delete(DbHelper dbHelper,string tableName)
        {
            _DbHelper = dbHelper;
            _TableName = tableName;
        }

        public Delete Where(string where)
        {
            _Where = where;
            return this;
        }

        public Delete Parameters(params object[] parameters)
        {
            if (parameters != null)
            {
                var _DynamicParameters = new DynamicParameters();
                for (var i = 0; i < parameters.Count(); i++) {
                    _DynamicParameters.Add("@" + i, parameters[i]);
                }

                _Parameters = _DynamicParameters;
            }
            
            return this;
        }

        public int Execute()
        {
            if (string.IsNullOrEmpty(_Where))
                _Where = " 1=1 ";

            string sql = string.Format(DELETE_SQL, _TableName, _Where);
            
            return _DbHelper.Execute(sql, _Parameters);
        }


    }
}
